//
//  Control.swift
//  TargetAction
//
//  Created by dayu on 15/5/29.
//  Copyright (c) 2015年 dayu. All rights reserved.
//

import Foundation
import UIKit

protocol TargetAction {
    func performAction();
}

struct TargetActionWrapper<T: AnyObject>: TargetAction {
    weak var target: T?;
    let action:(T) -> () -> ();
    
    func performAction() -> () {
        if let t = target {
            action(t)();
        }
    }
}

enum ControlEvent {
    case TouchUpInside
    case ValueChanged
    
}

class Control {
    var actions = [ControlEvent: TargetAction]();
    
    func setTarget<T: AnyObject>(target: T, action:(T)->()->(), controlEvent: ControlEvent) {
        actions[controlEvent] = TargetActionWrapper(target: target, action: action);
    }
    
    func removeTargetForControlEvent(controlEvent: ControlEvent) {
        actions[controlEvent] = nil;
    }
    
    func performActionForControlEvent(controlEvent: ControlEvent) {
        actions[controlEvent]?.performAction();
    }
}